package com.chefu.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chefu.admin.pojo.ServiceProvider;
import com.chefu.carowner.pojo.CarUser;
import com.chefu.carowner.pojo.UserCars;
import com.chefu.common.exception.CfException;
import com.chefu.common.exception.ExceptionEnum;
import com.chefu.common.pojo.PageResult;
import com.chefu.order.mapper.ServerOrderMapper;
import com.chefu.order.pojo.AutoPartsOrder;
import com.chefu.order.pojo.ServerOrder;
import com.chefu.order.pojo.vo.*;
import com.chefu.order.service.ServerOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import jdk.internal.org.objectweb.asm.TypeReference;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author goodgoodstudy
 * @since 2021-03-30
 */
@Service
public class ServerOrderServiceImpl extends ServiceImpl<ServerOrderMapper, ServerOrder> implements ServerOrderService {

    @Autowired
    private ServerOrderMapper serverOrderMapper;

    @Override
    public PageResult<ServerOrderPageVo> getOrderPage(Integer pageNo, Integer pageSize, ServerOrderPageParamVo paramVo) {

        PageResult<ServerOrderPageVo> result = new PageResult<>();
        Page<ServerOrder> serverOrderPage = new Page<>(pageNo, pageSize);
        QueryWrapper<ServerOrder> queryWrapper = new QueryWrapper<>();

        if (paramVo.getOrderId() != null) {
            queryWrapper.eq("so_id", paramVo.getOrderId());
        }
        if (!StringUtils.isEmpty(paramVo.getUserName())) {
            queryWrapper.like("user_name", paramVo.getUserName());
        }
        if (paramVo.getStatus() != null) {
            queryWrapper.eq("order_status", paramVo.getStatus());
        }
        if (paramVo.getPayStatus() != null) {
            queryWrapper.eq("pay_status", paramVo.getPayStatus());
        }
        if (paramVo.getlDate() != null) {
            if (paramVo.getrDate() != null) {
                if (paramVo.getlDate().getTime() < paramVo.getrDate().getTime()) {
                    queryWrapper.between("create_time", paramVo.getlDate(), paramVo.getrDate());
                } else {
                    throw new CfException(ExceptionEnum.TIME_INTERVAL_INCORRECT);
                }
            } else {
                queryWrapper.ge("create_time", paramVo.getlDate());
            }
        } else {
            if (paramVo.getrDate() != null) {
                queryWrapper.le("create_time", paramVo.getrDate());
            }
        }
        if (!StringUtils.isEmpty(paramVo.getServerProName())) {
            queryWrapper.like("server_pro_name", paramVo.getServerProName());
        }

        serverOrderMapper.selectPage(serverOrderPage, queryWrapper);
//        this.page(serverOrderPage,queryWrapper);
        result.setTotal(serverOrderPage.getTotal());
        result.setTotalPage(serverOrderPage.getPages());
        List<ServerOrder> records = serverOrderPage.getRecords();

        List<ServerOrderPageVo> items = new ArrayList<>();

        for (ServerOrder record : records) {
            ServerOrderPageVo serverOrderPageVo = new ServerOrderPageVo();
            BeanUtils.copyProperties(record, serverOrderPageVo);

            serverOrderPageVo.setStatus(record.getOrderStatus());
            serverOrderPageVo.setPayStatus(record.getPayStatus());
            serverOrderPageVo.setCreatTime(record.getCreateTime());

            items.add(serverOrderPageVo);
        }
        result.setItems(items);

        return result;
    }

    @Override
    public PageResult<ServerOrderPageVo> getPendingServerOrder(Integer pageNo, Integer pageSize, ServerOrderPageParamVo paramVo) {
        PageResult<ServerOrderPageVo> result = new PageResult<>();
        Page<ServerOrder> serverOrderPage = new Page<>(pageNo, pageSize);
        QueryWrapper<ServerOrder> queryWrapper = new QueryWrapper<>();

        if (paramVo.getOrderId() != null) {
            queryWrapper.eq("so_id", paramVo.getOrderId());
        }
        if (!StringUtils.isEmpty(paramVo.getUserName())) {
            queryWrapper.like("user_name", paramVo.getUserName());
        }

        if (paramVo.getlDate() != null) {
            if (paramVo.getrDate() != null) {
                if (paramVo.getlDate().getTime() < paramVo.getrDate().getTime()) {
                    queryWrapper.between("create_time", paramVo.getlDate(), paramVo.getrDate());
                } else {
                    throw new CfException(ExceptionEnum.TIME_INTERVAL_INCORRECT);
                }
            } else {
                queryWrapper.ge("create_time", paramVo.getlDate());
            }
        } else {
            if (paramVo.getrDate() != null) {
                queryWrapper.le("create_time", paramVo.getrDate());
            }
        }
        if (!StringUtils.isEmpty(paramVo.getServerProName())) {
            queryWrapper.like("server_pro_name", paramVo.getServerProName());
        }

        serverOrderMapper.selectPage(serverOrderPage, queryWrapper);
        result.setTotal(serverOrderPage.getTotal());
        result.setTotalPage(serverOrderPage.getPages());
        List<ServerOrder> records = serverOrderPage.getRecords();

        List<ServerOrderPageVo> items = new ArrayList<>();

        for (ServerOrder record : records) {
            ServerOrderPageVo serverOrderPageVo = new ServerOrderPageVo();
            BeanUtils.copyProperties(record, serverOrderPageVo);

            serverOrderPageVo.setStatus(record.getOrderStatus());
            serverOrderPageVo.setPayStatus(record.getPayStatus());
            serverOrderPageVo.setCreatTime(record.getCreateTime());

            items.add(serverOrderPageVo);
        }
        result.setItems(items);

        return result;
    }

    @Override
    public void closeServerOrder(Long orderId) {
        ServerOrder serverOrder = serverOrderMapper.selectById(orderId);
        if (serverOrder==null) throw new CfException(ExceptionEnum.ORDER_NOT_FOUND);
        if (serverOrder.getPayStatus()==0){
            serverOrder.setPayStatus(2);
            serverOrderMapper.updateById(serverOrder);
        }else {
            throw new CfException(ExceptionEnum.INVALID_ORDER_STATUS);
        }
    }

    @Override
    public ServerOrderDetailVo getOrderDetail(Long orderId) {
        ServerOrderDetailVo detailVo = new ServerOrderDetailVo();

        ServerOrder serverOrder = serverOrderMapper.selectById(orderId);
        detailVo.setSoId(orderId);
        detailVo.setCreateTime(serverOrder.getCreateTime());
        detailVo.setStatus(serverOrder.getOrderStatus());

        Long carId = serverOrder.getCarId();
        Long cuId = serverOrder.getCuId();
        //TODO 通过carId、cuId查询
        CarUser carUser = new CarUser();
        UserCars userCars = new UserCars();
        CarUserVo carUserVo = new CarUserVo();
        carUserVo.setName(carUser.getRealName());
        carUserVo.setAppointment(serverOrder.getAppointment());
        carUserVo.setNumberPlate(userCars.getCarNum());
        carUserVo.setEngineNumber(userCars.getEngineNum());
        carUserVo.setVINcode("LUAU2AUB3GE383467");
        carUserVo.setCarType(userCars.getSeries());

        detailVo.setCarUserVo(carUserVo);

        //TODO 通过spId查询服务商
        ServiceProvider serviceProvider = new ServiceProvider();
        ServerProVo serverProVo = new ServerProVo();
        serverProVo.setName(serviceProvider.getShortName());
        serverProVo.setContactNumber(serviceProvider.getPhone());
        serverProVo.setAddress(serviceProvider.getAddress());

        detailVo.setServerProVo(serverProVo);

        ObjectMapper objectMapper = new ObjectMapper();
        String item = serverOrder.getItem();
        List<ServerItemVo> serverItemList = null;
        try {
            serverItemList = objectMapper.readValue(item, List.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        detailVo.setServerItemList(serverItemList);

        List<OrderLogVo> orderLogVoList = new ArrayList<>();
        OrderLogVo orderLogVo = new OrderLogVo();
        orderLogVo.setCreateTime(new DateTime(serverOrder.getCreateTime()).plusDays(1).toDate());
        orderLogVo.setDesc("订单审核");
        orderLogVo.setStatus(serverOrder.getOrderStatus());
        orderLogVoList.add(orderLogVo);

        detailVo.setOrderLogVoList(orderLogVoList);

        return detailVo;
    }
}
